package com.zdkj.subway.st.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zdkj.common.core.util.SpringContextHolder;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.st.constant.enums.StockEnum;
import com.zdkj.subway.st.dto.in.SwStAuditDTO;
import com.zdkj.subway.st.entity.*;
import com.zdkj.subway.st.mapper.SwStPlanDayMapper;
import com.zdkj.subway.st.service.*;
import com.zdkj.subway.st.util.DateUtilsSt;
import com.zdkj.subway.st.util.GenSerialNo;
import com.zdkj.subway.st.util.JdbcUtil;
import com.zdkj.subway.st.util.SubwayStConstants;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

@Service
@AllArgsConstructor
public class SwStPlanDayServiceImpl extends ServiceImpl<SwStPlanDayMapper, SwStPlanDay> implements SwStPlanDayService {

    private final SwTkTicketDeliveryService swTkTicketDeliveryService;
    private final SwTkTicketDeliveryDetailService swTkTicketDeliveryDetailService;
    private final SwStTicketHandService swStTicketHandService;
    private final SwStTicketHandDetailService swStTicketHandDetailService;
    private final SwStInputDetailServiceImpl swStInputDetailService;
    private final SwStOutputDetailServiceImpl swStOutputDetailService;
    @Resource
    SwStInputDetailServiceImpl inputDetailServiceImpl;
    @Resource
    SwStOutputDetailServiceImpl outputDetailServiceImpl;
    @Autowired
    private SwStStockDayCheckDetailServiceImpl sdcd;
    @Autowired
    private GenSerialNo genSerialNo;
    @Override
    public boolean submitBatch(List<Integer> ids) {
        Collection<SwStPlanDay> swStPlanDays = listByIds(ids);
        for (SwStPlanDay swStPlanDay : swStPlanDays) {
            if(SubwayStConstants.AUDIT_STATUS_NOT_SUBMIT.equals(swStPlanDay.getCheckStatus())||
                    SubwayStConstants.AUDIT_STATUS_FAIL.equals(swStPlanDay.getCheckStatus())){
                swStPlanDay.setCheckSubmit(SubwayStConstants.SUBMIT_STATUS_PASS);
                swStPlanDay.setCheckStatus(SubwayStConstants.AUDIT_STATUS_WAIT);
            }
        }
        return updateBatchById(swStPlanDays);
    }
    @Override
    public boolean applyedBatch(List<Integer> ids) {
        Collection<SwStPlanDay> swStPlanDays = listByIds(ids);
        for (SwStPlanDay swStPlanDay : swStPlanDays) {
            if("1".equals(swStPlanDay.getCheckApproveStatus())||
                    "4".equals(swStPlanDay.getCheckApproveStatus())){
                swStPlanDay.setCheckApproveStatus("2");
            }
        }
        return updateBatchById(swStPlanDays);
    }
    @Override
    public boolean approvedBatch(List<Integer> ids) {
        Collection<SwStPlanDay> swStPlanDays = listByIds(ids);
        for (SwStPlanDay swStPlanDay : swStPlanDays) {
            if("2".equals(swStPlanDay.getCheckApproveStatus())){
                swStPlanDay.setCheckApproveStatus("3");
            }
        }
        return updateBatchById(swStPlanDays);
    }

    private int getInputSettleNum(SwStInputDetail swStInputDetail) {
        int settleNum = 0;
        int outStorageNum = sdcd.getOutStorgeNum(swStInputDetail.getTicketType(), swStInputDetail.getStockArea());
        int inStorageNum = sdcd.getInStorgeNum(swStInputDetail.getTicketType(), swStInputDetail.getStockArea());
        if (outStorageNum == 0 && inStorageNum == 0) {
            settleNum = 0;
        } else if (outStorageNum == 0 && inStorageNum != 0) {
            settleNum = inStorageNum;
        } else {
            //统计最新库存量 如果尚未尚未出库过，则库存为入库数
            settleNum = sdcd.getSettleNum(swStInputDetail.getTicketType(), swStInputDetail.getStockArea());
        }
        return settleNum;
    }
    @Transactional(rollbackFor = Exception.class)
    public boolean saveInputDetail(SwStInputDetail swStInputDetail) {
        boolean issuccuess = true;
        //首先更新库存账
        SwStStockDayCheckDetail swStStockDayCheckDetail = new SwStStockDayCheckDetail();
        try {
            //统计最新库存量 如果尚未尚未出库过，则库存为入库数
            int settleNum = this.getInputSettleNum(swStInputDetail);
            //设置票种
            swStStockDayCheckDetail.setTicketType(swStInputDetail.getTicketType());
            //车票名称
            swStStockDayCheckDetail.setTicketName(swStInputDetail.getTicketName());
            //车票次数
            swStStockDayCheckDetail.setTicketTimes(swStInputDetail.getTicketTimes());
            //车票金额
            swStStockDayCheckDetail.setTicketPrice(swStInputDetail.getTicketPrice());
            //出入库类型
            swStStockDayCheckDetail.setStorageType("入库");
            //出入库票价
            swStStockDayCheckDetail.setTicketPrice(swStInputDetail.getTicketPrice());
            //设置库存分区
            swStStockDayCheckDetail.setStockArea(swStInputDetail.getStockArea());
            //更新库存量
            swStStockDayCheckDetail.setStorageNumber(swStInputDetail.getTicketNumber());
            //更新结存量=最新库存量+当前出库
            swStStockDayCheckDetail.setSettleNumber(settleNum + swStInputDetail.getTicketNumber());
            issuccuess = sdcd.save(swStStockDayCheckDetail);
            if (!issuccuess) {
                issuccuess = false;
                throw new RuntimeException("库存更新异常！");
            }
            //其次更新入库基础表
            PigxUser user = SecurityUtils.getUser();
            swStInputDetail.setCreatorName(user.getUsername());
            swStInputDetail.setCreatorId(user.getId());
            //设置关联库存id
            swStInputDetail.setStockdayid(swStStockDayCheckDetail.getId());
            //未分解
            swStInputDetail.setBreakFlag("0");
            //其他关联方式入库，不需要重新设置流水号
            if (StringUtils.isEmpty(swStInputDetail.getInstockno())) {
                //根据票种生成单号前缀
                String prefix = genSerialNo.getPreFixByTicketType(swStInputDetail.getTicketType());
                String serialno = genSerialNo.genSerioNo(StockEnum.StockType.INSTOCK.getValue());
                swStInputDetail.setInstockno(prefix+serialno);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return issuccuess && swStInputDetailService.save(swStInputDetail);
    }
    @Override
    public boolean inputDetailSubmitBatch(List<Integer> ids) {
        PigxUser user = SecurityUtils.getUser();
        Collection<SwStPlanDay> swStPlanDays = listByIds(ids);
        boolean isSucss1 = true;
        boolean isSucss2 = true;
        boolean isSucss3 = true;
        List<SwStPlanDay> swStPlanDaysList = new ArrayList<>();
        List<SwStInputDetail> list = new ArrayList<>();
        for (SwStPlanDay swStPlanDay : swStPlanDays) {
            //库存分区测试的入库
            if(swStPlanDay.getTestNumber()!=null && swStPlanDay.getTestNumber()>0) {

				swStPlanDay.setIsInstock("1");
                SwStInputDetail swStInputDetail = new SwStInputDetail();
                if("2".equals(swStPlanDay.getPlanType())){
                    swStInputDetail.setInGoingType("2");
                }else if("1".equals(swStPlanDay.getPlanType())){
                    swStInputDetail.setInGoingType("2");
                }
                swStInputDetail.setInGoingTime(DateUtilsSt.newLocalDateTime());
                swStInputDetail.setKeepercode(swStPlanDay.getPersonId());
                swStInputDetail.setKeepername(swStPlanDay.getPersonName());
                swStInputDetail.setTicketType(swStPlanDay.getTicketType());
                swStInputDetail.setTicketName(swStPlanDay.getTicketName());
                swStInputDetail.setTicketTimes(swStPlanDay.getTicketTimes());
                swStInputDetail.setTicketPrice(swStPlanDay.getTicketPrice());
                swStInputDetail.setTicketNumber(swStPlanDay.getTestNumber());
                swStInputDetail.setActiveTime(Optional.ofNullable(swStPlanDay.getActiveTime()).orElse("0"));
                swStInputDetail.setLatestStartTime(swStPlanDay.getLatestStartTime());
                swStInputDetail.setTicketNo(swStPlanDay.getStartEndNo());
                swStInputDetail.setKeeper1code(user.getUserCode());
                swStInputDetail.setKeeper1name(user.getNikeName());
                swStInputDetail.setRemark(swStPlanDay.getRemark());
                swStInputDetail.setDataSource("0");
                //入库生成单号和更新基础表信息
                inputDetailServiceImpl.inputGenerateSingle(swStInputDetail);
                list.add(swStInputDetail);
                //isSucss1 = swStInputDetailService.save(swStInputDetail);
                swStPlanDaysList.add(swStPlanDay);
				//updateById(swStPlanDay);
            }
            //有效车票的入库
            if(swStPlanDay.getEffectiveNumber()!=null && swStPlanDay.getEffectiveNumber()>0){
				swStPlanDay.setIsInstock("1");
                SwStInputDetail swStInputDetail2 = new SwStInputDetail();
                if("2".equals(swStPlanDay.getPlanType())){
                    swStInputDetail2.setInGoingType("2");
                }else if("1".equals(swStPlanDay.getPlanType())){
                    swStInputDetail2.setInGoingType("2");
                }
                swStInputDetail2.setInGoingTime(DateUtilsSt.newLocalDateTime());
                swStInputDetail2.setKeepercode(swStPlanDay.getPersonId());
                swStInputDetail2.setKeepername(swStPlanDay.getPersonName());
                swStInputDetail2.setTicketType(swStPlanDay.getTicketType());
                swStInputDetail2.setTicketName(swStPlanDay.getTicketName());
                swStInputDetail2.setTicketTimes(swStPlanDay.getTicketTimes());
                swStInputDetail2.setTicketPrice(swStPlanDay.getTicketPrice());
                swStInputDetail2.setTicketNumber(swStPlanDay.getEffectiveNumber());
                swStInputDetail2.setActiveTime(Optional.ofNullable(swStPlanDay.getActiveTime()).orElse("0"));
                swStInputDetail2.setLatestStartTime(swStPlanDay.getLatestStartTime());
                swStInputDetail2.setTicketNo(swStPlanDay.getStartEndNo());
                swStInputDetail2.setKeeper1code(user.getUserCode());
                swStInputDetail2.setKeeper1name(user.getNikeName());
                swStInputDetail2.setRemark(swStPlanDay.getRemark());
                swStInputDetail2.setDataSource("0");
                //入库生成单号和更新基础表信息
                inputDetailServiceImpl.inputGenerateSingle(swStInputDetail2);
                list.add(swStInputDetail2);
                //isSucss2 = swStInputDetailService.save(swStInputDetail2);
                swStPlanDaysList.add(swStPlanDay);
				//updateById(swStPlanDay);
            }
            //无效车票的入库
            if(swStPlanDay.getInvalidNumber()!=null && swStPlanDay.getInvalidNumber()>0){
				swStPlanDay.setIsInstock("1");
                SwStInputDetail swStInputDetail3 = new SwStInputDetail();
                if("2".equals(swStPlanDay.getPlanType())){
                    swStInputDetail3.setInGoingType("2");
                }else if("1".equals(swStPlanDay.getPlanType())){
                    swStInputDetail3.setInGoingType("2");
                }
                swStInputDetail3.setInGoingTime(DateUtilsSt.newLocalDateTime());
                swStInputDetail3.setKeepercode(swStPlanDay.getPersonId());
                swStInputDetail3.setKeepername(swStPlanDay.getPersonName());
                swStInputDetail3.setTicketType(swStPlanDay.getTicketType());
                swStInputDetail3.setTicketName(swStPlanDay.getTicketName());
                swStInputDetail3.setTicketTimes(swStPlanDay.getTicketTimes());
                swStInputDetail3.setTicketPrice(swStPlanDay.getTicketPrice());
                swStInputDetail3.setTicketNumber(swStPlanDay.getInvalidNumber());
                swStInputDetail3.setActiveTime(Optional.ofNullable(swStPlanDay.getActiveTime()).orElse("0"));
                swStInputDetail3.setLatestStartTime(swStPlanDay.getLatestStartTime());
                swStInputDetail3.setTicketNo(swStPlanDay.getStartEndNo());
                swStInputDetail3.setKeeper1code(user.getUserCode());
                swStInputDetail3.setKeeper1name(user.getNikeName());
                swStInputDetail3.setRemark(swStPlanDay.getRemark());
                swStInputDetail3.setDataSource("0");
                //入库生成单号和更新基础表信息
                inputDetailServiceImpl.inputGenerateSingle(swStInputDetail3);
                list.add(swStInputDetail3);
                //isSucss3 = swStInputDetailService.save(swStInputDetail3);
                swStPlanDaysList.add(swStPlanDay);
				//updateById(swStPlanDay);
            }
        }
        //用于更新是否操作入库
        isSucss1 = updateBatchById(swStPlanDaysList);
        isSucss2 = swStInputDetailService.saveBatch(list);
        return isSucss1 && isSucss2;
    }

    private int getOutputSettleNum(SwStOutputDetail swStOutputDetail) {
        int settleNum = 0;
        int outStorageNum = sdcd.getOutStorgeNum(swStOutputDetail.getTicketType(), swStOutputDetail.getStockArea());
        int inStorageNum = sdcd.getInStorgeNum(swStOutputDetail.getTicketType(), swStOutputDetail.getStockArea());
        if (outStorageNum == 0 && inStorageNum == 0) {
            settleNum = 0;
        } else if (outStorageNum == 0 && inStorageNum != 0) {
            settleNum = inStorageNum;
        } else {
            //统计最新库存量 如果尚未尚未出库过，则库存为入库数
            settleNum = sdcd.getSettleNum(swStOutputDetail.getTicketType(), swStOutputDetail.getStockArea());
        }
        return settleNum;
    }
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOutputDetail(SwStOutputDetail swStOutputDetail) {
        boolean issuccuess = true;
        //首先更新库存账
        SwStStockDayCheckDetail swStStockDayCheckDetail = new SwStStockDayCheckDetail();
        try {
            //统计最新库存量
            int settleNum = this.getOutputSettleNum(swStOutputDetail);
            //设置票种
            swStStockDayCheckDetail.setTicketType(swStOutputDetail.getTicketType());
            //车票名称
            swStStockDayCheckDetail.setTicketName(swStOutputDetail.getTicketName());
            //车票次数
            swStStockDayCheckDetail.setTicketTimes(swStOutputDetail.getTicketTimes());
            //车票金额
            swStStockDayCheckDetail.setTicketPrice(swStOutputDetail.getTicketPrice());
            //设置流动后库存
            swStStockDayCheckDetail.setStockArea(swStOutputDetail.getStockArea());
            //设置出库类型
            swStStockDayCheckDetail.setStorageType("出库");
            //更新库存量
            swStStockDayCheckDetail.setStorageNumber(swStOutputDetail.getTicketNumber());
            //更新结存量=最新库存量-当前出库
            swStStockDayCheckDetail.setSettleNumber(settleNum - swStOutputDetail.getTicketNumber());
            issuccuess = sdcd.save(swStStockDayCheckDetail);
            if (!issuccuess) {
                issuccuess = false;
                throw new RuntimeException("库存更新异常！");
            }
            //其次更新入库基础表
            PigxUser user = SecurityUtils.getUser();
            swStOutputDetail.setCreatorName(user.getUsername());
            swStOutputDetail.setCreatorId(user.getId());
            //设置关联库存id
            swStOutputDetail.setStockdayid(swStStockDayCheckDetail.getId());
            //所有出库信息默认为否
            swStOutputDetail.setIsreceive("否");
            //其他关联方式入库，不需要重新设置流水号
            if (StringUtils.isEmpty(swStOutputDetail.getOutstockno())) {
                //根据票种生成单号前缀
                String prefix = genSerialNo.getPreFixByTicketType(swStOutputDetail.getTicketType());
                String serialno = genSerialNo.genSerioNo(StockEnum.StockType.OUTSTOCK.getValue());
                swStOutputDetail.setOutstockno(prefix+serialno);
            }
            issuccuess = issuccuess && swStOutputDetailService.save(swStOutputDetail);
            SwTkOutStockReceive swTkOutStockReceive = SpringContextHolder.getBean(SwTkOutStockReceive.class);
            issuccuess = issuccuess && swTkOutStockReceive.saveTicketReceiveInfo(swStOutputDetail);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return issuccuess;
    }
    @Override
    public boolean outputDetailSubmitBatch(List<Integer> ids) {
        PigxUser user = SecurityUtils.getUser();
        Collection<SwStPlanDay> swStPlanDays = listByIds(ids);
        List<SwStOutputDetail> list = new ArrayList<>();
        boolean isSucss1 = true;
        /*boolean isSucss2 = true;
        boolean isSucss3 = true;*/
        for (SwStPlanDay swStPlanDay : swStPlanDays) {
            //库存分区测试的出库
            if(swStPlanDay.getTicketNumber()!=null && swStPlanDay.getTicketNumber()>0) {
				swStPlanDay.setIsOutstock("1");
				
                SwStOutputDetail swStOutputDetail = new SwStOutputDetail();
                if("2".equals(swStPlanDay.getPlanType())){
                    swStOutputDetail.setOutGoingType("1");
                }else if("1".equals(swStPlanDay.getPlanType())){
                    swStOutputDetail.setOutGoingType("1");
                }
                swStOutputDetail.setOutGoingTime(DateUtilsSt.newLocalDateTime());
                swStOutputDetail.setKeepercode(swStPlanDay.getPersonId());
                swStOutputDetail.setKeepername(swStPlanDay.getPersonName());
                swStOutputDetail.setTicketType(swStPlanDay.getTicketType());
                swStOutputDetail.setTicketName(swStPlanDay.getTicketName());
                swStOutputDetail.setTicketTimes(swStPlanDay.getTicketTimes());
                swStOutputDetail.setTicketPrice(swStPlanDay.getTicketPrice());
                swStOutputDetail.setTicketNumber(swStPlanDay.getTicketNumber());
                swStOutputDetail.setActiveTime(Optional.ofNullable(swStPlanDay.getActiveTime()).orElse("0"));
                swStOutputDetail.setLatestStartTime(swStPlanDay.getLatestStartTime());
                swStOutputDetail.setTicketNo(swStPlanDay.getStartEndNo());
                swStOutputDetail.setKeeper1code(user.getUserCode());
                swStOutputDetail.setKeeper1name(user.getNikeName());
                swStOutputDetail.setRemark(swStPlanDay.getRemark());
                swStOutputDetail.setDataSource("0");
                //出库生成单号和更新基础表信息
                outputDetailServiceImpl.outputGenerateSingle(swStOutputDetail);
                list.add(swStOutputDetail);
                //isSucss1 = swStOutputDetailService.save(swStOutputDetail);
				//updateById(swStPlanDay);
            }
            //有效车票的入库
            /*if(swStPlanDay.getEffectiveNumber()!=null && swStPlanDay.getEffectiveNumber()>0){
                SwStOutputDetail swStOutputDetail2 = new SwStOutputDetail();
                swStOutputDetail2.setOutGoingType("1");
                swStOutputDetail2.setOutGoingTime(DateUtilsSt.newLocalDateTime());
                swStOutputDetail2.setKeepercode(swStPlanDay.getPersonId());
                swStOutputDetail2.setKeepername(swStPlanDay.getPersonName());
                swStOutputDetail2.setTicketType(swStPlanDay.getTicketType());
                swStOutputDetail2.setTicketName(swStPlanDay.getTicketName());
                swStOutputDetail2.setTicketTimes(swStPlanDay.getTicketTimes());
                swStOutputDetail2.setTicketPrice(swStPlanDay.getTicketPrice());
                swStOutputDetail2.setTicketNumber(swStPlanDay.getEffectiveNumber());
                swStOutputDetail2.setActiveTime(Objects.equals(swStPlanDay.getActiveTime(),"")||swStPlanDay.getActiveTime()==null?0:Integer.parseInt(swStPlanDay.getActiveTime()));
                swStOutputDetail2.setLatestStartTime(swStPlanDay.getLatestStartTime());
                swStOutputDetail2.setTicketNo(swStPlanDay.getStartEndNo());
                swStOutputDetail2.setKeeper1code(user.getUserCode());
                swStOutputDetail2.setKeeper1name(user.getNikeName());
                swStOutputDetail2.setRemark(swStPlanDay.getRemark());
                isSucss2 = saveOutputDetail(swStOutputDetail2);
            }*/
            //无效车票的入库
            /*if(swStPlanDay.getInvalidNumber()!=null && swStPlanDay.getInvalidNumber()>0){
                SwStOutputDetail swStOutputDetail3 = new SwStOutputDetail();
                swStOutputDetail3.setOutGoingType("1");
                swStOutputDetail3.setOutGoingTime(DateUtilsSt.newLocalDateTime());
                swStOutputDetail3.setKeepercode(swStPlanDay.getPersonId());
                swStOutputDetail3.setKeepername(swStPlanDay.getPersonName());
                swStOutputDetail3.setTicketType(swStPlanDay.getTicketType());
                swStOutputDetail3.setTicketName(swStPlanDay.getTicketName());
                swStOutputDetail3.setTicketTimes(swStPlanDay.getTicketTimes());
                swStOutputDetail3.setTicketPrice(swStPlanDay.getTicketPrice());
                swStOutputDetail3.setTicketNumber(swStPlanDay.getInvalidNumber());
                swStOutputDetail3.setActiveTime(Objects.equals(swStPlanDay.getActiveTime(),"")||swStPlanDay.getActiveTime()==null?0:Integer.parseInt(swStPlanDay.getActiveTime()));
                swStOutputDetail3.setLatestStartTime(swStPlanDay.getLatestStartTime());
                swStOutputDetail3.setTicketNo(swStPlanDay.getStartEndNo());
                swStOutputDetail3.setKeeper1code(user.getUserCode());
                swStOutputDetail3.setKeeper1name(user.getNikeName());
                swStOutputDetail3.setRemark(swStPlanDay.getRemark());
                isSucss3 = saveOutputDetail(swStOutputDetail3);
            }*/
        }
        //用于更新是否操作出库
        updateBatchById(swStPlanDays);
        return swStOutputDetailService.saveBatch(list);
    }

    @Override
    public boolean auditBatch(SwStAuditDTO swStAuditDTO,Integer id) {
        PigxUser user = SecurityUtils.getUser();
        Collection<SwStPlanDay> swStPlanDays = listByIds(swStAuditDTO.getIds());
        String status = swStAuditDTO.getResult()? SubwayStConstants.AUDIT_STATUS_PASS
                : SubwayStConstants.AUDIT_STATUS_FAIL;
        for (SwStPlanDay swStPlanDay : swStPlanDays) {
            if(SubwayStConstants.AUDIT_STATUS_WAIT.equals(swStPlanDay.getCheckStatus())){
                swStPlanDay.setCheckStatus(status);
                swStPlanDay.setCheckSuggest(swStAuditDTO.getSuggestions());
                swStPlanDay.setCheckPersonId(user.getId());
				swStPlanDay.setCheckPersonName(user.getNikeName());
                swStPlanDay.setCheckTime(LocalDateTime.now());
            }
        }
        if("2".equals(status) && id ==3){
            //日计划配送管理到车票配送主表
            SwTkTicketDelivery ticketDelivery = new SwTkTicketDelivery();
            SwTkTicketDeliveryDetail ticketDeliveryDetail = new SwTkTicketDeliveryDetail();
            boolean saveDelivery = false;
            for (SwStPlanDay swStPlanDay : swStPlanDays) {
                //日计划配送管理到车票配送主表
                ticketDelivery.setLineCode(swStPlanDay.getLineCode());
                ticketDelivery.setStationCode(swStPlanDay.getStationCode());
                ticketDelivery.setDeliveryCount(swStPlanDay.getTicketNumber());
                ticketDelivery.setDeliveryPersonName(swStPlanDay.getPersonName());
                ticketDelivery.setDeliveryPersonCode(swStPlanDay.getPersonId());
                ticketDelivery.setDeliveryPersonTwoName(swStPlanDay.getPersonTwoName());
                ticketDelivery.setDeliveryPersonTwoCode(swStPlanDay.getPersonTwoId());
                ticketDelivery.setDeliveryTime(LocalDateTime.now());
                ticketDelivery.setDataSource("1");
                ticketDelivery.setDeptId(user.getDeptId());
                ticketDelivery.setCreatorId(user.getId());
                ticketDelivery.setCreatorName(user.getNikeName());
                ticketDelivery.setCreateTime(LocalDateTime.now());
                saveDelivery = swTkTicketDeliveryService.save(ticketDelivery);
                if (saveDelivery = true) {
                    //日计划配送管理到车票配送明细表
                    ticketDeliveryDetail.setRefId(ticketDelivery.getId());
                    ticketDeliveryDetail.setTicketType(swStPlanDay.getTicketType());
                    ticketDeliveryDetail.setTicketTypeParam(swStPlanDay.getTicketName());
                    ticketDeliveryDetail.setNumberInterval(swStPlanDay.getStartEndNo());
                    ticketDeliveryDetail.setTicketAmount(swStPlanDay.getTicketPrice());
                    ticketDeliveryDetail.setTicketCount(swStPlanDay.getTicketTimes().toString());
                    ticketDeliveryDetail.setDeliveryCount(swStPlanDay.getTicketNumber());
                    swTkTicketDeliveryDetailService.save(ticketDeliveryDetail);
                }
            }
        }
        if("2".equals(status) && id ==4){
            //日计划管理回收管理审批通过到车票回收
            SwStTicketHand swStRecovery = new SwStTicketHand();
            //日计划配送管理到车票回收明细表
            SwStTicketHandDetail swStTicketHandDetail = new SwStTicketHandDetail();
            for (SwStPlanDay swStPlanDay : swStPlanDays) {
                //日计划管理回收管理审批通过到车票回收
                swStRecovery.setLineCode(swStPlanDay.getLineCode());
                swStRecovery.setStationCode(swStPlanDay.getStationCode());
                swStRecovery.setReceivePersonName(swStPlanDay.getPersonName());
                swStRecovery.setReceivePersonCode(swStPlanDay.getPersonId());
                swStRecovery.setReceivePersonTwoName(swStPlanDay.getPersonTwoName());
                swStRecovery.setReceivePersonTwoCode(swStPlanDay.getPersonTwoId());
                swStRecovery.setReceiveTime(LocalDateTime.now());
                swStRecovery.setDataSource("0");
                swStRecovery.setReceiveCount(swStPlanDay.getTicketNumber());
                swStRecovery.setSubmitStatus("1");
                swStRecovery.setNoticeStatus("0");
                swStRecovery.setNoticeConfirm("0");
                swStRecovery.setHandStatus("1");
                swStRecovery.setRemark(swStPlanDay.getRemark());
                swStRecovery.setCreatorName(swStPlanDay.getCreatorName());
                swStRecovery.setCreatorId(user.getId());
                swStRecovery.setDeptId(user.getDeptId());
                boolean re = swStTicketHandService.save(swStRecovery);
                if(re = true){
                    swStTicketHandDetail.setRefId(swStRecovery.getId());
                    swStTicketHandDetail.setTicketType(swStPlanDay.getTicketType());
                    swStTicketHandDetail.setTicketName(swStPlanDay.getTicketName());
                    swStTicketHandDetail.setNumberInterval(swStPlanDay.getStartEndNo());
                    swStTicketHandDetail.setTicketAmount(swStPlanDay.getTicketPrice());
                    swStTicketHandDetail.setTicketCount(swStPlanDay.getTicketTimes().toString());
                    swStTicketHandDetail.setReceiveCount(swStPlanDay.getTicketNumber());
                    swStTicketHandDetail.setRemark(swStPlanDay.getRemark());
                    swStTicketHandDetailService.save(swStTicketHandDetail);
                }
            }
        }
        return updateBatchById(swStPlanDays);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delByPlanWeekId(Long id) {
        try {
            JdbcUtil.exeSqlCrud("delete from sw_tk_public_plan_day where PLAN_WEEK_ID = " + id);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return true;
    }
}
